<!-- 首页 -->
<template>
	<view>
		<!-- 轮播图 -->
		<view class="home">
			<u-swiper img-mode="scaleToFill" :list="list" :effect3d="true" :circular="true" height="300" @click="change"></u-swiper>
		</view>
		<!--    <view>-->
		<!--      <button class="btn" open-type="share">立即邀请好友</button>-->
		<!--    </view>-->
		<!-- 帮组 -->
		<view class="advertising">
			<image class="advertisingImg" mode="scaleToFill" :src="bangzuImg" @click="inform()"></image>
		</view>

		<!-- 附近推荐文字 -->
		<view class="font-home">附近推荐：</view>
		<!-- 附件推荐的用户 -->
		<view v-for="(item,index) in userList" class="userView" @click="jumpPage(item)" :key="index">
			<view class="UserData" :class="[item.sex == 2 ? 'boy' : 'girl']">
				<!-- 照片层 -->
				<view class="userImg">
					<image mode="aspectFill" class="userImg" :src="IMG_URL + item.links[0].link"></image>
				</view>
				<!-- 文字层 -->
				<view style="width: 100%;">
					<!-- 个人信息 -->
					<view class="briefly">
						<view class="UserName" :class="[item.sex == 2 ? 'boyMoney':'girlMoney']">
							{{ item.name }}
						</view>
						<view style="font-size: 15px;width: 4rem;margin-left: 15rpx;"
							:class="[item.sex == 2 ? 'boyMoney':'girlMoney']">{{ item.age }}岁
						</view>
					</view>
					<!-- 简介 -->
					<view class="userbriefly">
						<p class="userbriefly">{{ item.signature }}</p>
					</view>
					<!-- 标签 -->
					<view class="tag-user">
						<view class="tag">
							<u-tag v-if="item.title[0] != null" color="white" bg-color="#FF7F50" border-color="#FF7F50"
								:text="item.title[0]" type="success" size="mini"></u-tag>
						</view>
						<view class="tag">
							<u-tag v-if="item.title[1] != null" color="white" bg-color="#FF7F50" border-color="#FF7F50"
								:text="item.title[1]" type="success" size="mini"></u-tag>
						</view>
					</view>
					<!-- 金额数目 -->
					<view class="boxWarp">
						<view class="boxWarpMoney" :class="[item.sex == 2 ? 'boyMoney':'girlMoney']">
							<image class="imageContent" mode="aspectFit" :src="[item.sex == 2 ? imgBlue : imgPink]">
							</image>
							{{ item.price + "币/小时" }}
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
		<view>
		</view>
	</view>
</template>

<script>
	import {
		getGamerByUserId,
		nearbyGamer
	} from "../../api/gamer";
	import {
		getBanner
	} from "../../api/home";
	import {
		getCode,
		wx_login
	} from "../../api/login";
	import {
		getShop
	} from "../../api/business";
	import {
		createRoom,
		delRoom,
		getRoomByUserId
	} from "../../api/room";
	import {
		IMG_URL
	} from "../../config/URL";

	export default {
		// 下拉刷新
		onPullDownRefresh() {
			uni.stopPullDownRefresh()
		},

		async created() {
			const {
				code
			} = await getCode()
			const loginMsg = await wx_login(code)
			if (loginMsg.data.msg != '用户不存在') {
				uni.setStorageSync('token', loginMsg.data.data.token);
				uni.setStorageSync('userInfo', loginMsg.data.data.user);
				uni.setStorageSync('couponNum', loginMsg.data.data.couponNum);
				uni.setStorageSync('realName', loginMsg.data.data.isAttestation);
				//判断用户是不是互娱师
				const gamer = await getGamerByUserId({
					id: uni.getStorageSync('userInfo').id
				})

				if (gamer.data.code != 500) {
					uni.setStorageSync('gamerInfo', gamer.data.data);
				} else {
					uni.removeStorageSync('gamerInfo');
				}
				const shop = await getShop({
					f_user_id: uni.getStorageSync('userInfo').id
				})

				if (shop.data.code != 500) {
					uni.setStorageSync('shopInfo', shop.data.data);
				} else {
					uni.removeStorageSync('shopInfo');
				}

			} else {
				uni.removeStorageSync('token');
				uni.removeStorageSync('userInfo');
				uni.navigateTo({
					url: '/subPages/subRegister/index'
				})
			}
      await this.fetch(this.page);
      await this.getMessage()

		},

		async onShow() {
			await this.fetch(this.page);
			await this.getMessage()
		},
		data() {
			return {
				IMG_URL,
				status: '',
				bangzuImg: "/static/image/swiper/bangzu.png",
				imgBlue: "/static/image/userDetails/boyCoin.png",
				imgPink: "/static/image/userDetails/girlCoin.png",
				advertisingImg: "/static/image/swiper/swiper1.png",
				userList: [],
				isShow: false,
				isAgree: false,
				page: 1,
				judge: 0,
				list:[]
			}
		},

		methods: {
			async onReachBottom() {
				this.page += 1
				await this.fetch(this.page)
			},

			//通知
			inform() {
				uni.showToast({
					title: '功能开发中，敬请期待~',
					duration: 2000,
					icon: 'none',
				});
			},

			//发起帮主
			async setBangzu() {
				// const asd =  await delRoom()

				//判断用户有没有加入房间
				const room = await getRoomByUserId({
					checkId: uni.getStorageSync('userInfo').id
				});


				//用户没有加入房间
				if (room.data.code == 500) {
					uni.showModal({
						title: '提示',
						content: '是否发起帮组',
						success: async function(res) {
							if (res.confirm) {
								//创建房间
								const res = await createRoom()
								if (res.data.code == 500) {
									uni.showToast({
										title: res.data.data.msg,
										duration: 2000,
										icon: 'none',
									});
									return
								}
								let data = {
									roomId: res.data.data,
									type: 'create'
								}
								uni.navigateTo({
									url: '/subPages/group/group?data=' + encodeURIComponent(JSON
										.stringify(data))
								})
							} else if (res.cancel) {}
						}
					});
				} else {
					//已加入房间则带着房间号码跳转
					let data = {
						roomId: room.data.data,
						type: 'create'
					}
					uni.navigateTo({
						url: '/subPages/group/group?data=' + encodeURIComponent(JSON.stringify(data))
					})
				}

			},

			async fetch(y) {
				uni.showLoading({
					title: "加载中"
				})
				const res = await nearbyGamer({
					page: y,
					limit: 10
				});
				if (res.data.data.length == 0) {} else if (this.judge == 0) {
					res.data.data.map(e => {
						this.userList.push(e)
					})
					this.judge = res.data.data[0].id
				} else if (this.judge != res.data.data[0].id) {
					res.data.data.map(e => {
						this.userList.push(e)
					})
				}

				uni.hideLoading();

			},

			// 轮播图跳转广告页
			async change(index) {
				// 第几张图
			},
			// 广告
			async getMessage() {
				const banner = await getBanner()
				let link = banner.data.data;
				// console.log(link,"link");
				link.map(e => {
					this.list.push({
						image: IMG_URL + e.image
					})
				})
				this.list.length = link.length
			},
			// 页面跳转
			jumpPage(item) {
				let data = {
					gamer: item,
					Userid: item.id,
				}
				uni.navigateTo({
					// 参数传递
					url: "/pages/user/userDetails?data=" + encodeURIComponent(JSON.stringify(data))
				})
			},

		}
	}
</script>

<!-- 开启scss -->
<style lang="scss" scoped>
	.imageContent {
		width: 50rpx;
		height: 50rpx;
	}

	.imgModel {
		width: 120rpx;
		height: 120rpx;
	}

	.boxWarp {
		width: 100%;
	}

	.UserName {
		font-size: 40rpx;
		white-space: nowrap;
	}

	.userView {
		border-radius: 20px;
		margin-top: 20px;
	}

	// 变色层
	.boy {
		background: linear-gradient(white, #6CC7E2);
	}

	.girl {
		background: linear-gradient(white, #EFA7D2);
	}

	// 金额色彩
	.boyMoney {
		color: #09B6EA;
	}

	.girlMoney {
		color: #FF1493;
	}

	.boxWarpMoney {
		font-size: 1rem;
		margin-top: 10px;
		text-align: right;
		margin-right: 20px;
		float: right;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tag {
		margin-left: 8px;
		margin-top: 5px;
	}

	.tag-user {
		display: flex;
		flex-wrap: wrap;
	}

	.advertising {
		display: flex;
		justify-content: center;
		margin-top: 2rem;
		align-items: center;
		height: 160rpx;
	}

	.advertisingImg {
		height: 100%;
		width: 100%;
	}

	.font-home {
		font-size: 1.1rem;
		margin: 1rem;
		font-weight: 900;
	}

	.UserData {
		border-radius: 20px;
		margin: 20rpx;
		display: flex;
		height: 130px;
	}

	.userImg {
		border-radius: 20px;
		height: 20px;
		width: 9rem;
		height: 130px;
	}

	.briefly {
		display: flex;
		align-items: center;
		flex-direction: row;
		margin: 0.5rem;
		color: #FF1493;
	}

	.userbriefly {
		margin: 0.2rem;
		font-size: 12px;
	}
</style>
